package advert_api

import (
	"github.com/gin-gonic/gin"
	"zswei.xyz/manager/global"
	"zswei.xyz/manager/models"
	"zswei.xyz/manager/models/res"
)

type AdvertRequest struct {
	Title  string `json:"title" binding:"required" desc:"请输入标题"`     // 显示的标题
	Href   string `json:"href" binding:"required" desc:"请输入跳转连接"`    // 跳转链接
	Images string `json:"images" binding:"required" desc:"选择图片"`     // 图片
	IsShow *bool  `json:"is_show" binding:"required" desc:"请选择是否展示"` // 是否展示
}

// AdvertCreateView 添加广告
//	@Tags			广告管理
//	@Summary		创建广告
//	@Description	创建广告
//	@Param			data	body	AdvertRequest	true	"创建广告参数"
//	@Router			/v2/advert/createAdvert [post]
//	@Produce		json
//	@Success		200	{object}	res.Response{}
func (AdvertApi) AdvertCreateView(c *gin.Context) {
	var cr AdvertRequest
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithErrorDesc(err, &cr, c)
		return
	}

	// 查询是否有相同广告
	var advertModel models.AdvertModel
	err = global.DB.Take(&advertModel, "title = ?", cr.Title).Error
	if err == nil {
		res.FailWithDesc("广告名称已存在，换一个试试", c)
		return
	}

	// 创建广告
	err = global.DB.Create(&models.AdvertModel{
		Title:  cr.Title,
		Href:   cr.Href,
		Images: cr.Images,
		IsShow: *cr.IsShow,
	}).Error
	if err != nil {
		res.FailWithDesc("创建广告失败", c)
		return
	}

	res.OkWith(c)
}
